(function( $ ){
  $.fn.fastinquiry = function( options ) {  
	// Create some defaults, extending them with any options that were provided
	var settings = $.extend( {
		'attribute' : 'id',
		'typeSeparator' : '~',
		'submitButtonText' : '',
		'submitButtonClass' : 'fi-submit-class',
		'maxOfferTitleChars' : 25,
		'top' : 0,
		'left' : 0,
		'maxItems' : 3,//0 means unlimited
		'closeText' : '',
		'showCheckBox' : true,
		'autoCheck' : true,
		'headingClass' : 'fi-heading-text',
		'headingText' : 'Send Quick Inquiry',
		'checkboxClass' : 'fi-checkbox',
		'closeClass' : 'fi-close-button',
		'offerTitleClass' : 'fi-label',
		'offerCheckName' : 'select_offer', //don't change unless u know what you are doing
		'textareaClass' : 'fi-message',
		'defaultMessageText' : '',
		'textareaName' : 'message', //don't change unless u know what you are doing
		'minMessageChars' : 25,
		'minMessageError' : 'Atleast 25 charachters message is required',
		'nothingCheckedError' : 'No product selected. At least check one.',
		'enableSendQuotation' : true,
		'sendQuotationName' : 'send_sim', //don't change unless u know what you are doing
		'sendQuotationText' : 'Send quotations of similar offers',
		'sendQuotationClass' : 'fi-label',
		'placeholder' : 'Insert your message here',
		'wrapperClass' : 'fi-wrapper',
		'messageWrapperClass' : 'fi-contactnow',
		'offerWrapperClass' : 'fi-offer-wrapper',
		'submitAction' : 'inquiry_inquire',
		'siteIndex' : 'index.html',
		'errorClass' : 'fi-error',
		'event' : 'mouseover',
		'hiddenFields' : [// you can also put category_ids, title, subject and keywords here in name value pairs
			{'name' : 'fast_inquiry', 'value' : 1},
			{'name' : 'first_time', 'value' : 1},
			{'name' : 'subject', 'value' : 'Aladee baba dus dus'},
			{'name' : 'keywords', 'value' : 'shoes, nike, naswar, cotton'},
			{'name' : 'categories_ids', 'value' : '1217'},
			{'name' : 'title', 'value' : 'bunder bunder tu hai bunder'}
		]
	}, options);
	tipHtml = '<div class="' + settings.wrapperClass + '" id="fi-wrapper" style="display:none;z-index:3000;"><form id="fi-form" action="/' + settings.siteIndex + '"><a href="javascript:;" id="fi-close" class="' + settings.closeClass + '">' + settings.closeText + '</a><span class=' + settings.headingClass +'>' + settings.headingText +'</span>';
	tipHtml += '<div id="fi-offer-wrapper" class="' + settings.offerWrapperClass + '"></div>';
	tipHtml += '<div class="' + settings.messageWrapperClass + '"><textarea id="fi-message" class="' + settings.textareaClass + '" name="' + settings.textareaName + '" placeholder="' + settings.placeholder + '">' + settings.defaultMessageText + '</textarea>';
	if(settings.enableSendQuotation == true)
	{
		tipHtml += '<label class="' + settings.sendQuotationClass + '"><input checked="checked" type="checkbox" name="' + settings.sendQuotationName + '" value="1" />' + settings.sendQuotationText + '</label>';
	}
	tipHtml += '<input id="fi-submit-button" class="' + settings.submitButtonClass + '" type="submit" value="' + settings.submitButtonText + '"/><div id="fi-error" style="display:none;" class="' + settings.errorClass +'"></div>';
	tipHtml += '<input type="hidden" name="action" value="'+ settings.submitAction + '">';
	for(index=0;index<settings.hiddenFields.length;index++)
	{
		tipHtml += '<input type="hidden" name="'+ settings.hiddenFields[index].name + '" value="' + settings.hiddenFields[index].value + '">';
	}
	tipHtml += '</form></div>';
	$('body').prepend(tipHtml);
	if(settings.event != 'mouseover')
	{
		settings.event = 'click';
	}
	mouseout = false;//save this variable for hiding widget after some time
	$('#fi-wrapper').hover(function(){
		mouseout = false;
	},
	function(){
		mouseout = true;
		window.setTimeout(function(){//wait to check if mouse went to link
			if(mouseout)
			{
				$('#fi-wrapper,#fi-error').hide();
			}
		}, 1500);
	});
	this.bind(settings.event, function(){
		mouseout = false;
		id = $(this).attr('id');
		if(typeof id != 'undefined')
		{
			temp = id.split(settings.typeSeparator);
			type = temp[1];
			id = temp[0];			
			not_exists = ($('#offer_' + id).length > 0 ? false : true);
			if(id > 0 && type > 0 && not_exists )//check if valid inputs
			{
				if(settings.maxItems != 0 && $("." + settings.checkboxClass).length == settings.maxItems)
				{
					$("." + settings.checkboxClass + ":first").parent().remove();
				}
				title = '';
				if($(this).find('img').length != 0)
				{
					title = $(this).attr('title');
				}
				else
				{
					title = $(this).text();
				}
				if(title.length > settings.maxOfferTitleChars)
				{
					title = title.substring(0, settings.maxOfferTitleChars) + '...';
				}
				checkbox_style = '';
				if(settings.showCheckBox == false)
				{
					checkbox_style = 'style="visibility:hidden;"';
				}
				$('#fi-offer-wrapper').append('<label id="label_' + id + '" class="' + settings.offerTitleClass + '"><input ' + checkbox_style + ' class="' + settings.checkboxClass + '" type="checkbox" id="offer_' + id + '" value="' + id + '~' + type + '" name="' + settings.offerCheckName +'\[\]" checked="checked">' + title + '</label>');
			}
			if(settings.showCheckBox)
			{
				$('.' + settings.offerTitleClass).css("background-color", "");
				label = $('#offer_' + id).parent();
				label.css("background-color", "#FFFF99");
			}			
			if(settings.autoCheck == false)
			{		
				$("." + settings.checkboxClass).not('#offer_' + id).removeAttr('checked');
				$('#offer_' + id).attr("checked", "checked");
			}
		}
		if($('.' + settings.checkboxClass).length > 0)
		{
			offsetTop = $(this).offset().top - $('#fi-wrapper').height() - 5;
			offsetLeft = $(this).offset().left - 5;
			if(offsetTop < 5)//make it appear in bottom
			{
				offsetTop = $(this).offset().top + $(this).height() + 5;
			}
			if(offsetLeft < 5)//make it appear to right
			{
				offsetLeft = $(this).offset().left + 5;
			}			
			offsetTop += settings.top;
			offsetLeft += settings.left;
			$('#fi-wrapper').css({'top' : offsetTop, 'left' : offsetLeft, 'position':'absolute'}).show();
		}
	}).bind('mouseout', function(){
		mouseout = true;
		window.setTimeout(function(){//wait to check if mouse went to widget
			if(mouseout)
			{
				$('#fi-wrapper,#fi-error').hide();
			}
		}, 1500);
	});
	$('#fi-submit-button').click(function(){
		error = '<ul>';
		if($("#fi-message").val().length < settings.minMessageChars)
		{
			error += '<li>' +settings.minMessageError + '</li>';
		}
		if($("." + settings.checkboxClass + ":checked").length < 1)
		{
			error += '<li>' + settings.nothingCheckedError + '</li>';
		}
		if(error != '<ul>')
		{
			$("#fi-error").html(error + '</ul>').show();
			return false;
		}
		else
		{
			$("#fi-form").submit();
			return true;
		}
	});	
	$this = this;
	$('#fi-close').click(function(){
		$this.unbind(settings.event).unbind('mouseout');
		$('#fi-wrapper').remove();
	});
	return this;
  };
})( jQuery );
